program xcompositeqt;

{$mode objfpc}{$H+}

uses

  CMem,Strings,
  SysUtils,Classes,Types,Math,qt4;


function SlotName(Name:String):PAnsiChar;
begin
Result:=StrNew(PChar('1'+Name));
end;

function SignalName(Name:String):PAnsiChar;
begin
Result:=StrNew(PChar('2'+Name));
end;


type
  TMainForm = class;

  TCircleWidget = class(TObject)
  private
    Handle : QWidgetH;
    FFloatBased : Boolean;
    FAntialiased : Boolean;
    FFrameNo : Integer;
    procedure SetFloatBased (const Value : Boolean);
    procedure SetAntiAliased (const Value : Boolean);
    procedure NextAnimationFrame;cdecl;
    function EventFilter(Sender: QObjectH; Event: QEventH): Boolean; cdecl;
    procedure Paint (QPaintEvent : QPaintEventH);
  public  
    constructor Create(Parent:TMainForm);
    property FloatBased : Boolean read FFloatBased write SetFloatBased;
    property Antialiased : Boolean read FAntialiased write SetAntialiased;
  end;

  TMainForm = class(TObject)
  private
    CircleWidgets : array [1..2,1..2] of TCircleWidget;
    function CreateLabel (s : WideString) : QLabelH;
  public  
    Handle : QWidgetH;
    quitButton : QPushButtonH;
    constructor Create;
  end;

  TApplication = class(TObject)
  private
  public  
    MainForm : TMainForm;
    constructor Create;
  end;  
  



//*******************************************************
// TCircleWidget
//*******************************************************


//===============================================
constructor TCircleWidget.Create(Parent:TMainForm);
var
  Hook   : QWidget_hookH;
begin
inherited Create();

Handle:=QWidget_create(Parent.Handle);

FFloatBased:=false;
FAntialiased:=false;
FFrameNo:=0;

QWidget_setBackgroundRole(Handle, QPaletteBase);
QWidget_setSizePolicy(Handle, QSizePolicyExpanding, QSizePolicyExpanding);

// Event filter
Hook:=QWidget_hook_create(handle);
QObject_hook_hook_events(Hook,@EventFilter);
end;

//===============================================
procedure TCircleWidget.SetFloatBased (const Value : Boolean);
begin
FFloatbased:=Value;
QWidget_update(Handle);
end;

//===============================================
procedure TCircleWidget.SetAntiAliased (const Value : Boolean);
begin
FAntialiased:=Value;
QWidget_update(Handle);
end;

//===============================================
procedure TCircleWidget.NextAnimationFrame;cdecl;
begin
FFrameNo:=FFrameNo+1;
QWidget_update(Handle);
end;

//===============================================
function TCircleWidget.EventFilter(Sender: QObjectH; Event: QEventH): Boolean; cdecl;
begin
Result:=False;
if QEvent_type(Event) = QEventPaint then Paint(QPaintEventH(Event));
end;

//===============================================
procedure TCircleWidget.Paint (QPaintEvent : QPaintEventH);
var
  Painter : QPainterH;
  Diameter : Integer;
  Alpha, Delta : Integer; 
  QPen : QPenH;
  QColor : TQColor;
  QRectF : QRectFH; 
begin

try
  Painter:=QPainter_create(QWidget_to_QPaintDevice(Handle));
  QPainter_setRenderHint(Painter,QPainterAntialiasing, Antialiased);
  QPainter_translate(Painter,QWidget_width(Handle) div 2, QWidget_height(Handle) div 2);

  Diameter:=0;
  while Diameter < 256 do
    begin
    Delta := abs((FFrameNo mod 128) - Diameter div 2);
    Alpha := 255 - (Delta * Delta) div 4 - Diameter;
    if (Alpha > 0) then
       begin
       QColor_fromRGB(@QColor,0, Diameter div 2, 127, Alpha);
       QPen := QPen_create(@QColor);
       QPen_setWidth(QPen, 3);
       QPainter_setPen(painter, QPen);

       if FloatBased then
          begin
          QRectF:=QRectF_create(-diameter / 2.0, -diameter / 2.0,
                                           Diameter, Diameter);
          QPainter_drawEllipse(painter,QRectF);
          QRectF_destroy(QRectF);
          end
       else 
          QPainter_drawEllipse(painter,-diameter div 2, -diameter div 2,
                                           Diameter, Diameter);

       end;
    Diameter:=Diameter+9;
    end;

finally
  QPen_destroy(QPen);
  QPainter_destroy(Painter);
end;

end;


//*******************************************************
// TMainForm
//*******************************************************
  
  
//===============================================
constructor TMainForm.Create;
var
  w      : Widestring;
  i, j   : integer;
  layout : QGridLayoutH;
  QTimer : QTimerH;
  TimerHook : QTimer_hookH;
begin
inherited;

Handle:=QWidget_create();

layout := QGridLayout_create;
QGridLayout_addWidget(layout,CreateLabel('Aliased'),     0, 1);
QGridLayout_addWidget(layout,CreateLabel('Antialiased'), 0, 2);
QGridLayout_addWidget(layout,CreateLabel('Int'),         1, 0);
QGridLayout_addWidget(layout,CreateLabel('Float'),       2, 0);

QTimer:=QTimer_create(Self.Handle);

for i:=1 to 2 do
  for j:=1 to 2 do
     begin
     circleWidgets[i][j]:=TCircleWidget.Create(Self);
     circleWidgets[i][j].Antialiased := j <> 1;
     circleWidgets[i][j].FloatBased  := i <> 1;

     TimerHook:=QTimer_hook_create(QTimer);
     QTimer_hook_hook_timeout(TimerHook,@circleWidgets[i][j].NextAnimationFrame);

     QGridLayout_addWidget(layout,circleWidgets[i][j].Handle, i, j);
     end;

QTimer_start(QTimer, 100);

QWidget_setLayout(Handle,layout);

w:='Concentric Circles';
QWidget_setWindowTitle(Handle,@w);

QWidget_resize(Handle,600,600);
QWidget_show(Handle);

end;


//===============================================
function TMainForm.CreateLabel (s : WideString) : QLabelH;
begin

Result := QLabel_create(@s);

QLabel_setAlignment(Result, QtAlignCenter);
QLabel_setMargin(Result, 2);
QFrame_setFrameStyle(Result, Integer(QFrameBox) or Integer(QFrameSunken));

end;

//*******************************************************
// TApplication    
//*******************************************************


//===============================================
constructor TApplication.Create;
begin
inherited;
QApplication_Create(@argc,argv);
MainForm:=TMainForm.Create;
QApplication_Exec();
end;





//*******************************************************
// Main
//*******************************************************
begin
writeln('start of program');
TApplication.Create;
writeln('end of program');
end.
